package com.czl.module_service.fragment.purchase

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.PurchaseOrderBean
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.PurchaseApplyCardInfoAdapter
import com.czl.module_service.databinding.*
import com.czl.module_service.fragment.approvalFlow.ApprovalFlowFragment
import com.czl.module_service.viewmodel.PurchaseApplyInfoViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_PURCHASE_APPLY_INFO)
class PurchaseApplyInfoFragment : ApprovalFlowFragment<FragmentPurchaseApplyInfoBinding, PurchaseApplyInfoViewModel>() {
    override fun initContentView(): Int {
        return R.layout.fragment_purchase_apply_info
    }
//    override fun useBaseLayout(): Boolean {
//        return false
//    }
    private val footerBinding: FragmentPurchaseApplyInfoFooterBinding by lazy  {
    FragmentPurchaseApplyInfoFooterBinding.inflate(layoutInflater).apply {

        }
    }

    private val mAdapter: PurchaseApplyCardInfoAdapter by lazy {
        PurchaseApplyCardInfoAdapter(this).apply {
            val headerBinding = FragmentPurchaseApplyInfoHeaderBinding.inflate(layoutInflater)
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
            val centerFooterBinding = FragmentPurchaseApplyInfoCenterFooterBinding.inflate(layoutInflater)
            centerFooterBinding.setVariable(initVariableId(), viewModel)
            addFooterView(centerFooterBinding.root)
            footerBinding.setVariable(initVariableId(), viewModel)
            addFooterView(footerBinding.root)
        }
    }
    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("采购申请详情")
        viewModel.applicaitonId =
            arguments?.getString("applicaitonId")?.toInt()
        viewModel.tabIndex =
            arguments?.getString("tabIndex")?.toInt()!!
        viewModel.taskUserId =
            arguments?.getString("taskUserId")
        viewModel.WXGetApplicationInfo(applicaitonId = viewModel.applicaitonId)
        initAdapter()
    }

    override fun dataResult(processStatus: Int?, taskStatus: Int?) {
        if (viewModel.tabIndex == 2 && taskStatus == 2) {
            footerBinding.llBtn1.visibility = View.GONE
            footerBinding.llBtn2.visibility = View.VISIBLE
        } else {
//            footerBinding.llBtn1.visibility = View.GONE
            footerBinding.llBtn2.visibility = View.GONE
        }
    }

    private fun initAdapter() {
        binding.ryCommon.run {
            adapter = mAdapter

        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            if (data.status != 12) {
                super.initViewObservable()
            }
            if (viewModel.tabIndex == 1 && data.status == 1) {
                footerBinding.llBtn1.visibility = View.VISIBLE
                footerBinding.llBtn2.visibility = View.GONE
            } else {
                footerBinding.llBtn1.visibility = View.GONE
//                footerBinding.llBtn2.visibility = View.GONE
            }
            initFlowData(0, viewModel.applicaitonId!!, viewModel.taskUserId!!)
            viewModel.setData(data)
            mAdapter.setList(data.projectList as MutableList<PurchaseOrderBean>?)
        })
        viewModel.uc.backCompleteEvent.observe(this, {
            back()
        })
    }

    override fun getAdapter(): BaseQuickAdapter<*, *> {
        return mAdapter
    }

    override fun addIndex(): Int {
        return 1
    }
}